package com.seadragon.app.ksr.controller;

import static com.seadragon.app.ksr.model.KsrConstants.ENTITY_TYPE_BUSINESS;
import static com.seadragon.app.ksr.model.KsrConstants.ENTITY_TYPE_CUSTOMER;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seadragon.app.ksr.model.Customer;
import com.seadragon.app.ksr.model.Office;
import com.seadragon.app.ksr.model.Review;
import com.seadragon.app.ksr.service.CustomerService;
import com.seadragon.app.ksr.service.OfficeService;
import com.seadragon.app.ksr.service.ReviewService;
import com.seadragon.app.ksr.util.GsonUtils;

@Controller
public class ReviewController {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private OfficeService officeService;

	@Autowired
	private ReviewService reviewService;
	

	@RequestMapping(value = "/{entityType}/review/{entityId}/list/{rating}.json", method = RequestMethod.GET)
	public @ResponseBody
	String getReviewListJson4(HttpServletRequest request, HttpSession session,@PathVariable String entityType,@PathVariable Long entityId,@PathVariable Integer rating) {
		return reviewListJsonByEntity(entityType,entityId, rating, null, null);
	}
	
	@RequestMapping(value = "/{entityType}/review/{entityId}/list/{offset}/{pagesize}.json", method = RequestMethod.GET)
	public @ResponseBody
	String getReviewListJson5(HttpServletRequest request, HttpSession session,@PathVariable String entityType,@PathVariable Long entityId,@PathVariable Integer offset,@PathVariable Integer pagesize) {
		return reviewListJsonByEntity(entityType,entityId, null, offset, pagesize);
	}
	
	@RequestMapping(value = "/{entityType}/review/{entityId}/list/{rating}/{offset}/{pagesize}.json", method = RequestMethod.GET)
	public @ResponseBody
	String getReviewListJson6(HttpServletRequest request, HttpSession session,@PathVariable String entityType,@PathVariable Long entityId,@PathVariable Integer rating,@PathVariable Integer offset,@PathVariable Integer pagesize) {
		return reviewListJsonByEntity(entityType,entityId, rating, offset, pagesize);
	}	

	@RequestMapping(value = "/{entityType}/review/{entityId}/list.json", method = RequestMethod.GET)
	public @ResponseBody
	String getReviewListJsonByBusiness(HttpServletRequest request, HttpSession session,@PathVariable String entityType,@PathVariable Long entityId) {
		return reviewListJsonByEntity(entityType,entityId, null, null, null);
	}
	
	
	private String reviewListJsonByEntity (String entityType,Long entityId,Integer rating,Integer offset,Integer pagesize){
		String json = null;
		List<Review> reviewList = null;
		Map<String,List<Review>> reviewListByRatings = null;
		if (ENTITY_TYPE_BUSINESS.equals(entityType)){
			Office office =officeService.findById(entityId);
			   if (office!=null){
				 if (rating!=null&&(rating==100||rating==80||rating==60||rating==40||rating==20)){
					 reviewList =reviewService.findByOfficeAndReviewRating(office, rating);
				 } else if (rating!=null&&rating == 0){ //all ratings with each in a list
					 reviewListByRatings = new HashMap<>();
					 reviewListByRatings.put("rating100", reviewService.findByOfficeAndReviewRating(office, 100));
					 reviewListByRatings.put("rating80", reviewService.findByOfficeAndReviewRating(office, 80));
					 reviewListByRatings.put("rating60", reviewService.findByOfficeAndReviewRating(office, 60));
					 reviewListByRatings.put("rating40", reviewService.findByOfficeAndReviewRating(office, 40));
					 reviewListByRatings.put("rating20", reviewService.findByOfficeAndReviewRating(office, 20));
				 } else
				   reviewList =reviewService.findByOffice(office);
			   }
		} else if (ENTITY_TYPE_CUSTOMER.equals(entityType)){
			Customer customer =customerService.findById(entityId);
			   if (customer!=null){
				   if (rating!=null&&(rating==100||rating==80||rating==60||rating==40||rating==20)){
					   reviewList =reviewService.findByCustomerAndReviewRating(customer, rating);
				   }else if (rating!=null&&rating == 0){ //all ratings with each in a list
					 reviewListByRatings = new HashMap<>();
					 reviewListByRatings.put("rating100", reviewService.findByCustomerAndReviewRating(customer, 100));
					 reviewListByRatings.put("rating80", reviewService.findByCustomerAndReviewRating(customer, 80));
					 reviewListByRatings.put("rating60", reviewService.findByCustomerAndReviewRating(customer, 60));
					 reviewListByRatings.put("rating40", reviewService.findByCustomerAndReviewRating(customer, 40));
					 reviewListByRatings.put("rating20", reviewService.findByCustomerAndReviewRating(customer, 20));
				 } else {
					 reviewList =reviewService.findByCustomer(customer);
				 }
			   }
		}
		if (reviewListByRatings!=null)
			json =GsonUtils.ObjectToGson(reviewListByRatings);
		else if (reviewList!=null)
			json =GsonUtils.ObjectToGson(reviewList);
		return json;
	}



	@RequestMapping(value = "/review/save.json", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject createOffice(@RequestBody String reviewJson,HttpSession session) {
		Review review = (Review)GsonUtils.GsonToObject(reviewJson, Review.class);
		// save office
		if (review.getId()>0)
			reviewService.update(review);
		else
			reviewService.save(review);
		
		JSONObject json = new JSONObject();
		json.put("status", "ok");
		return json;
	}
   
}

